/**
 * 
 */
package com.yidin.lsc;

import java.io.ByteArrayOutputStream;
import java.util.HashMap;
import java.util.Map;

import com.yidin.entry.LoginInfo;
import com.yidin.entry.RegisterInfo;
import com.yidin.logic.MainService;
import com.yidin.logic.Task;
import com.yidin.widget.Carema;

import android.app.AlertDialog;
import android.content.ContentResolver;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

/**
 * @author cola
 * 
 */
public class Register extends BaseActivity {
	public static Register self;
	private Views[] views;
	private ViewPager pager;
	private Bitmap myBitmap;
	private byte[] mContent;
	private ViewPagerAdapter pagerControl;// 子页面控件
	private String AccountId = "";
	private String Password = "";

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_lsc);
		this.findView();
		this.init();
		views = new Views[1];
		views[0] = new RegisterView(this);

		this.pagerControl = new ViewPagerAdapter(this, views);
		this.pager.setAdapter(this.pagerControl);
		this.pager.setCurrentItem(0);
		this.self = this;
	}

	private void findView() {
		this.pager = (ViewPager) findViewById(R.id.ViewPager);

	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		if (myBitmap != null)
			myBitmap.recycle();
		ContentResolver resolver = getContentResolver();
		/**
		 * 因为两种方式都用到了startActivityForResult方法， 这个方法执行完后都会执行onActivityResult方法，
		 * 所以为了区别到底选择了那个方式获取图片要进行判断，
		 * 这里的requestCode跟startActivityForResult里面第二个参数对应
		 */
		if (requestCode == 0) {
			try {
				if (data == null) {
					finish();
					return;
				}
				// 获得图片的uri
				Uri originalUri = data.getData();
				// 将图片内容解析成字节数组
				mContent = Carema.readStream(resolver.openInputStream(Uri
						.parse(originalUri.toString())));
				// 将字节数组转换为ImageView可调用的Bitmap对象
				myBitmap = Carema.getPicFromBytes(mContent, null);

			} catch (Exception e) {
				System.out.println(e.getMessage());
			}
		} else if (requestCode == 1) {
			try {
				super.onActivityResult(requestCode, resultCode, data);
				Bundle extras = data.getExtras();
				myBitmap = (Bitmap) extras.get("data");
				if (myBitmap == null)
					return;
				ByteArrayOutputStream baos = new ByteArrayOutputStream();
				myBitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
				mContent = baos.toByteArray();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				finish();
			}
		}
		((RegisterView) views[0]).setImage(myBitmap);
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			final AlertDialog.Builder builder = new AlertDialog.Builder(this);
			builder.setCancelable(false);
			builder.setTitle("温馨提示");
			builder.setMessage("是否退出大小圈");
			builder.setPositiveButton("是的",
					new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog,
								int whichButton) {
							setResult(1);
							finish();
							// System.exit(1);
						}
					});
			builder.setNegativeButton("返回",
					new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog,
								int whichButton) {
						}
					});
			builder.show();
			return false;
		}
		return super.onKeyDown(keyCode, event);
	}

	@Override
	public void init() {
		// TODO Auto-generated method stub

	}

	@Override
	public void refresh(Object... param) {
		// TODO Auto-generated method stub
		dismissProgress();

		int task = (Integer) param[0];
		switch (task) {
		case Task.TASK_USER_REGISTER: {
			RegisterInfo info = (RegisterInfo) param[1];
			if (info.e.equals("0000"))
				((RegisterView) views[0]).registerResult(true, info.e);
			else
				((RegisterView) views[0]).registerResult(false, info.e);
			break;
		}
		case Task.TASK_USER_LOGIN: {
			LoginInfo info = (LoginInfo) param[1];
			if (info.e.equals("0000")) 
					((RegisterView) views[0]).registerResult(true, info.e);
				else
					((RegisterView) views[0]).registerResult(false, info.e);
				break;
			}
		case Task.TASK_RESET_PASSWORD:{
			String info = (String) param[1];
			if (info.equals("0000")) 
					((RegisterView) views[0]).resetPassword(true, info);
				else
					((RegisterView) views[0]).resetPassword(false, info);
			break;
		}
		}
	}

	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}
}
